package util;

import java.sql.*;
import java.util.ResourceBundle;

public class JDBCutil {
    private static  final String DIRVER;
    private static  final String URL;
    private static  final String USERNAME;
    private static  final String PAW;

    /**
     * 获取连接对象
     */
    static {
        ResourceBundle jdbc = ResourceBundle.getBundle("jdbc");
        DIRVER= jdbc.getString("jdbc.driverName");
        URL= jdbc.getString("jdbc.url");
        USERNAME= jdbc.getString("jdbc.username");
        PAW= jdbc.getString("jdbc.password");
        try {
           Class.forName(DIRVER);
        } catch (ClassNotFoundException e) {
            throw new RuntimeException(e);
        }
    }
    public static Connection getConnection() {
        Connection connection=null;
        try {
            connection = DriverManager.getConnection(URL, USERNAME, PAW);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        }
        return connection;
    }
    public static void closeAll(ResultSet resultSet, Statement statement, Connection connection) {
        if (connection != null) {
            try {
                connection.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (resultSet != null) {
            try {
                resultSet.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
        if (statement != null) {
            try {
                statement.close();
            } catch (SQLException e) {
                throw new RuntimeException(e);
            }
        }
    }
}
